package pkg.hicph.org;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class MemoriesRoom extends Activity implements OnClickListener {

	Button cancelButton;
	TextView title, info;

	int achievedMemoriesCount = 0;
	private ListView lv;
	String[] MEMORIES = new String[Utils.totalMemories];
	private ArrayAdapter<String> arrayMemories;
	
	private OnItemClickListener memoryClickedCallback;
	
	// Preference file
    public static final String PREF_FILE = "pf_file";
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(
        WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.memoriesroom);
        
        
        achievedMemoriesCount = 0;
        
        Log.d("M.R.","TOTAL MEMO " + Utils.totalEvents);
        
        for(int j = 0; j < Utils.totalEvents; j++){
        	if(Utils.globalEvents[j].achieved){
        		achievedMemoriesCount++;
        	}
        }
        
        MEMORIES = new String[achievedMemoriesCount+Utils.currentDay];
        
        Log.d("M.R.","ARRAY CREATED " + achievedMemoriesCount);
        
        int g = 0;
        
        for(int i = 0; i < Utils.totalEvents; i++){
        	
        	Log.d("M.R.",Utils.globalEvents[i].achieved+", g: " + g + ", i: " + i);
        	
        	if(Utils.globalEvents[i].achieved){
        		MEMORIES[g] = Utils.globalEvents[i].name;
        		g++;
        	}

        }
        
        String[] auxStories = Utils.loadedStories(this);
        
        for(int a = 0; a < Utils.currentDay; a++){
        	int b = a+1;
        	MEMORIES[g+a] = "Your Day " + b; 
        }
        
        
        
        Log.d("M.R.","MEMORIES COUNT " + g);
        
        
        arrayMemories = new ArrayAdapter<String>(getBaseContext(), R.layout.itemsmemories, MEMORIES);
         	
        
        lv = (ListView) findViewById(R.id.memories_list);
        
        Log.d("M.R.","LV LOADED");

        lv.setTextFilterEnabled(false);
        lv.setItemsCanFocus(true);
        
        
        lv.setAdapter(arrayMemories);
        
        Log.d("M.R.","SET ADAPTER");
        
        registerViews();
        setupOnClickListeners();
        
        Log.d("M.R.","CONTAINERS");
        
        int aux = Utils.totalEvents+Utils.currentDay;
        
        title.setText("Memories room");
        info.setText("You have " + MEMORIES.length + " out of " + aux + " possible memories.");
        
    }
    
    public void updateMemories(){
    	
    	lv.refreshDrawableState();
  
    }
    
    public void memoryItemClick(View button){
    	Log.d("button","btn clicked" + lv.getPositionForView(button));
    	
    	Button aux = (Button) button;
    	
    	String[] auxStories = Utils.loadedStories(this);
    	String formattedStory;
    	
    	int auxIndex = lv.getPositionForView(button) - achievedMemoriesCount; 
    	
    	// is day story
    	if(auxIndex>=0){
    		
    		formattedStory = auxStories[auxIndex].trim();
    		
    		int ble = auxIndex+1;
    		
    		Utils.activeEvent = new Event(0, "Your day " + ble, "day", 0, true, formattedStory);
    		
    	}
    	else{
    		Utils.activeEvent = Utils.getEventByName(aux.getText()+"");
    	}
    	
    	
    	
    	Intent descriptionIntent = new Intent(this, MemoryDescriptionScreen.class);
		this.startActivity(descriptionIntent);
    	
    }

    private void registerViews() {
    	
    	title 		 = (TextView)this.findViewById(R.id.memories_room_title);
    	info 		 = (TextView)this.findViewById(R.id.memories_room_info);
    	cancelButton = (Button)this.findViewById(R.id.memories_room_cancel_button);
    	
	}

	public void setupOnClickListeners() {
        
		cancelButton.setOnClickListener(this);

    }
    
    public void onClick(View V) {
    	
    	switch(V.getId()) {
    	
    	case R.id.memories_room_cancel_button:
    		
    		this.finish();
    		
    		break;


    	}

    }
    

}
